let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
import { storeBindingsBehavior } from "mobx-miniprogram-bindings";
import member from "@src/store/member";
import base from "@src/store/base";
import { getCategoryDetail,checkSuperMemberStatus } from "@src/api/member";
import { parseCategoryIntro, parseCategoryRights, parsePrivileges, parseRechargeAwardRule } from "@src/common/memberCardUtils";
import router from "@src/router";

Component({
  behaviors: [storeBindingsBehavior, router.routerBehavior],
  storeBindings: [{
    store: member,
    fields: ['isMember', 'memberName', 'phone', 'categoryName', 'categoryImage'],
    actions: ['getBaseInfo'],
  }, {
    store: base,
    fields: ['themeColor', 'themeTitleColor']
  }],
  data: {
    instructions: [],
	// 超级会员
	superMemberStatus:null //0已开通    80034未开通
  },
  methods: {
    onLogin() {
      this.fetchData();
	  this.checkSuperMemberStatusFun()
    },
    fetchData() {
      getCategoryDetail().then(res => {
        let category = res.data.category;
        let privileges = res.data.privileges;
        let rechargeAward = res.data.rechargeAward;
        let introList = parseCategoryIntro(category)
        let categoryRights = parseCategoryRights(category, rechargeAward).concat(parseRechargeAwardRule(rechargeAward));
        console.log('categoryRights', categoryRights)
        privileges = parsePrivileges(privileges);
        this.handleEntry(category.instructions);
        this.setData({ categoryRights, introList, privileges })
        this.addCustomBenefit(res.data);

      })
    },

    //追加自定义权限
    addCustomBenefit(data) {
      let categoryRights = this.data.categoryRights;
      if (data.category.customBenefit) {
        let benefit = JSON.parse(data.category.customBenefit);
        console.log('benefit', benefit);
        benefit.forEach(element => {
          let obj = {
            title: element.value,
            iconName: 'icon_book'
          }
          categoryRights.push(obj);
        });
      }
      if (categoryRights.length > 0) {
        categoryRights.forEach(item => {
          if (item.iconName) { if (item.iconName.substr(0, 4) == "icon") { item.icon = true } else { item.icon = false } }
        })
      }
      this.setData({ categoryRights })
    },
    handleShowShops(e) {
      let shopIds = e.currentTarget.dataset.shop;
      this.selectComponent("#shop-list").open(shopIds);
    },
	handleSuperShowShops() {
		this.selectComponent("#superShop-list").open();
	},
    //识别换行符
    handleEntry(str) {
      if (str) {
        let strs = str.replace(/[\r\n\s]/g, "|-&");
        let arr = strs.split("|-&");
        this.setData({
          instructions: arr
        })
      }
    },
	checkSuperMemberStatusFun() {
		let businessId = extConfig.businessId;
		let from = {businessId}
		checkSuperMemberStatus(from).then(res => {
			console.log('商家是否开通超级会员',res)
			this.setData({
				superMemberStatus:res.data
			})
		})
	}
  }
})
